define(function(require,exports,module){	
	var tpl = require('../../tpl/member/my_wineVoucher.tpl');//载入tpl模板
	var curId = $("#my_wineVoucherDiv");
    var pagerid = "kkpagerMy_wineVoucher";
	//初始化显示表格、搜索
	require.async(['./showTable','./search'],function(showTable,search){
		search.search('/member/redpacket/availableWineCouponsJSON.html?',tpl,curId,pagerid);
		$(".my_wineVoucher").click(function(){
			showTable.ajaxUrl('/member/redpacket/availableWineCouponsJSON.html?status=' + 99,tpl,curId,pagerid);
			$("[name='my_wineCardCheckBoxAll']").prop("checked",false);
		});
	});
	
	$("[name='my_wineCardCheckBoxAll']").change(function(){
		if($(this).attr("checked")!=undefined){
			$("[name='my_wineVoucherCB']:enabled").prop("checked",true) ;
		}else{
			$("[name='my_wineVoucherCB']:enabled").prop("checked",false);
		}
	});	
	$(".wineVoucherBtn").click(function(){
		var getCheckedV = $("[name='my_wineVoucherCB']:checked");
		var len = getCheckedV.length;
		var voucherIds = [];
		if(len==0||len%6!=0){
			layer.alert("请选择6或6的倍数张的酒卡！");
		}else{
			for(var i=0;i<len;i++){
				voucherIds.push(getCheckedV[i].value);
			}
			$.ajax({
				url:"/member/redpacket/wineExchange.html",
				traditional:true,
				data:{"wineCouponsIds":voucherIds},
				success:function(data){
					layer.alert(data.msg);
					if(data.result){
						$(".my_wineVoucher").trigger("click");
					}
				}
			});
		}
	});
	$(".wineVoucherToMoneyBtn").click(function(){
		var getCheckedV = $("[name='my_wineVoucherCB']:checked");
		var len = getCheckedV.length;
		var voucherIds = [];
		
		if(len==0){
			layer.alert("请先选择酒券！");
			return;
		}
		for(var i=0;i<len;i++){
			if($(getCheckedV[i]).attr("data-val")=="0"){
				layer.alert("您勾选的第"+(i+1)+"张酒券处于不可兑换现金状态！");
				return;
			}
			voucherIds.push(getCheckedV[i].value);
		}
		layer.confirm('是否兑换选中的酒券?', {icon: 3, title:'提示'}, function(index){
			$.ajax({
				type: "post",
		        url: "/member/redpacket/wineCouponsExchange.html",
				traditional:true,
				data:{'wineCouponsIds':voucherIds,'_page_token':$("#_page_token").html()||''},
	            success: function(data){
	            	layer.alert(data.msg,{closeBtn:0},function(){
	            		window.location.reload();
	            	});
	            }
			});
			layer.close(index);
		});
	});
	
	//
	/*$(".wineVoucherTable").delegate(".WineCouponsExchange","click",function(){
		var wineId = [];
		wineId.push($(this).attr("data-val"));
		layer.confirm('是否兑换酒券?', {icon: 3, title:'提示'}, function(index){
			$.ajax({
	            type: "post",
	            url: "/member/redpacket/wineCouponsExchange.html",
	            traditional:true,
	            data:{'wineCouponsIds':wineId,'_page_token':$("#_page_token").html()||''},
	            success: function(data){
	            	layer.alert(data.msg,{closeBtn:0},function(){
	            		window.location.reload();
	            	});
	            	if(data.result){
	            		$(".my_wineVoucher").trigger("click");
	            	}
	            }
	        });
			layer.close(index);
		});
	})*/
	
});